package carTest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @description:
 * @author: 张德长 dechang.zhang@ucarinc.com
 * @date: 9:31:46 2018/11/1
 * @version:2.0
 */
public class StartCopy {
    public static String userName = "dechang.zhang";
    public static String pass = "ZdC199110";
    public static String httpTreeId = "172";//http文档节点id
    public static String rpcTreeId = "141";//rpc文档节点id
    public static String requestUrlStart = "/action/";//请求链接开头
    public static Set<String> excludeRequest = new HashSet<>();

    static {
        //请求参数中需要排除的字段
        excludeRequest.add("workerNo");
    }

    public static void main(String[] args) {

        Html html = new Html();
        String url = "http://cartest.10101111.com/usermanage/login.do";
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("name", userName));
        list.add(new BasicNameValuePair("password", pass));
        html.postHtml(url, list);
        url = "http://cartest.10101111.com/usermanage/findUserId?userName=" + userName;
        JSONObject userInfo = JSONObject.parseObject(html.getHtml(url));
        System.out.println(html.getHtml(url));

        url = "http://cartest.10101111.com/interface/getInterfacePage";
        // 获取http详情
        JSONObject jsonParam = new JSONObject();
        jsonParam.put("tree_id", httpTreeId);
        jsonParam.put("currentPage", "1");
        jsonParam.put("pageCount", "1000");
        // System.out.println(html.postJsonHtml(url,jsonParam));
        JSONArray array = html.postJsonHtml(url, jsonParam).getJSONArray("rows");
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            url = "http://cartest.10101111.com/interface/delete?interfaceId=" + jsonObject.get("id");
            html.getHtml(url);
        }
        url = "http://cartest.10101111.com/interface/getInterfacePage";
        // 获取RPC详情
        jsonParam = new JSONObject();
        jsonParam.put("tree_id", rpcTreeId);
        jsonParam.put("currentPage", "1");
        jsonParam.put("pageCount", "1000");
        //System.out.println(html.postJsonHtml(url,jsonParam));
        array = html.postJsonHtml(url, jsonParam).getJSONArray("rows");
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            url = "http://cartest.10101111.com/interface/getInterfaceInfo?interfaceId=" + jsonObject.get("id");
            //System.out.println(html.getHtml(url));
            JSONObject rpc = (JSONObject) JSONObject.parse(html.getHtml(url));
            JSONObject rpcDoc = rpc.getJSONObject("re");
            //System.out.println("查找出的rpc"+rpcDoc);
            String serviceName = rpcDoc.getString("serviceid");
            String methodName = rpcDoc.getString("method");
            Doc formatDoc = JSONObject.parseObject(rpcDoc.toString(), Doc.class);


            formatDoc.description = formatDoc.description + "\n服务名称：" + serviceName + "\n调用方法：" + methodName;
            formatDoc.interfaceType = 1;
            formatDoc.interfaceTreeId = httpTreeId;
            //formatDoc.userId = userInfo.getString("id");
            String[] s = serviceName.split("\\.");
            if (s.length > 2) {
                formatDoc.requestUrl = requestUrlStart + s[s.length - 2] + "/" + s[s.length - 1];
            } else {
                formatDoc.requestUrl = requestUrlStart + serviceName + "/" + methodName;
            }
            formatDoc.rpc2httpParam(excludeRequest);
            // System.out.println("转换后的http"+JSONObject.toJSON(formatDoc));
            url = "http://cartest.10101111.com/interface/add";
            JSONObject response = html.postJsonHtml(url, (JSONObject) JSONObject.toJSON(formatDoc));
            if ("success".equals(response.getString("result"))) {
                //System.out.println(response);
            } else {
                System.out.println("COPY 出错：" + JSONObject.toJSON(formatDoc));
            }
        }
        System.out.println("COPY 结束：");

       /* url = "http://cartest.10101111.com/interface/getInterfaceInfo?interfaceId=1639";
        System.out.println(html.getHtml(url));
        JSONObject rpc = (JSONObject)JSONObject.parse(html.getHtml(url));
        Doc r =  JSONObject.parseObject(rpc.get("re").toString(),Doc.class);
        System.out.println(JSONObject.toJSON(r));*/

    }
}
